import java.io.*;

//JAXP packages

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/*SAX uses the Simple API for XML (SAX) to parse a given XML 
 * document then print its element nodes
 * */

/*
 * Author: Piyush Garg
 * Student ID: S3218012
 * Assignment3: Document Markup Language
 */

public class SAXPrint extends DefaultHandler
{
	private int levelCount;
	public SAXPrint() {
	}

	// DefaultHandler methods 
	
	@Override
	/*Called when the document has started.
	 * */
	public void startElement(String uri, String lName, String qName, Attributes attribs) throws SAXException {
		// TODO Auto-generated method stub
		System.out.println();
		if(qName.compareTo("orders") != 0)
		{
            for(int i = 0 ; i < levelCount ; i++)
            {
                System.out.print("|");
            }
		}
		
		System.out.print("+-"+qName);
		
		if(qName.compareTo("author") != 0)
		{
            levelCount++;
		}
		
		//This part is for the satndard Length
		if (attribs.getLength() > 0) 
		{       
			System.out.print("(");
			for (int i = 0; i < attribs.getLength(); i ++) 
			{            
				System.out.print(attribs.getQName(i) + "=" +attribs.getValue(i));   
				if(i!=1)
				{
					System.out.print(",");
				}
			}  
			System.out.print(")");
		 }   
	}
	
	
	@Override
	/*Called when element data has been parsed.
	 * */   
	public void characters(char[] data, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		//Storing all the Characters in String Buffers
		StringBuffer buf = new StringBuffer(length);      
		buf.append(data, start, length);     
		
		//Printing the value of each element
	 	if (buf.toString().trim().length() > 0) 
		{        
			 System.out.print(":" + buf);      
		}  
	}

	@Override
	/* Called when an element has ended.
	 * */   
	public void endElement(String uri, String name, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(qName.compareTo("author") != 0)
            levelCount--;

        if(qName.compareTo("order") == 0)
            levelCount = 1;
	}

	/*Print the given document.    */   
	private void print(File docFile) 
	{     
		 try 
		{   // Get an instance of parser factory and configure it.         
			SAXParserFactory factory = SAXParserFactory.newInstance();         
		
			// Get a parser from the factory.         
			
			SAXParser parser = factory.newSAXParser();         
			
			// Parse the input file and print the elements.        
			 parser.parse(docFile, this);
			System.out.println();
		} catch(Exception ex) 
		{        
		 	System.out.println(ex);     
		}  
	 }   
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SAXPrint handler = new SAXPrint();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        
        // If not enough args, print usage.      
			if (args.length < 1) 
			{         
				System.out.println("Usage: SaxPrint file");         
				System.exit(0);      
			}
			System.out.println();      
			System.out.println(args[0]);
			// Get file name from command-line.      
			File docFile = new File(args[0]);      
			// Instantiate class and print.      
			(new SAXPrint()).print(docFile);   
         
	}

}
